'''
Created on Dec 6, 2009

@author: bodnaria
'''

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from model.TempUser import createTempUser
from google.appengine.api import mail
from error import UserError
import os
import re
from util import BBPage

def validateEmail(email):

    if len(email) > 7:
        if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) != None:
            return True
    return False


    
class RegisterPage(BBPage):
    def post(self):
        user = self.request.get("regusernmae")
        email = self.request.get("regemail")
        
        #if no valid email promt user with error message
        if  not validateEmail(email):
            self.redirect("/?error=badEmail")
            return            
        
        try: 
            createTempUser(user, #username                          
                       email, #email 
                       )
        except UserError.UserError, e:
            self.redirect('/?error='+ e.value)
            return
        
        self.outputHeader()
        
        templatePath = os.path.join(os.path.dirname(__file__), 'register.html')
        self.response.out.write(template.render(templatePath, {} ))
        return
    
    def get(self):
        self.post()
    
 




    